<template>
  <div class="importCreditOpen p20">
    <!--table布局-->
    <el-form ref="form" :model="form" :rules="rules" class="formEmbedTable" v-if="!success">
      <table>
        <tr style="height:60px;">
          <td>Operator Name</td>
          <td colspan="3">
            <el-form-item>
              {{userName}}
            </el-form-item>
          </td>
        </tr>
        <tr style="height:60px;">
          <td>old password</td>
          <td colspan="3">
            <el-form-item prop="password">
              <el-input v-model="form.password" ref="password" style="width:300px;" type="password"></el-input>
            </el-form-item>
          </td>
        </tr>
        <tr style="height:60px;">
          <td>new password</td>
          <td colspan="3">
            <el-form-item prop="password1">
              <el-input v-model="form.password1" ref="password1" style="width:300px;" type="password"></el-input>
            </el-form-item>
            <!--el-form-item style="width:355px;"><span class="cRed">登录密码为8至12位的字符（必须同时包含字母、数字、特殊字符中两种或以上的类型组合，字母区分大小写)</span></el-form-item-->
            <!--el-tag type="success">密码长度8-12位,不能为纯数字</el-tag-->
          </td>
        </tr>
        <tr style="height:60px;">
          <td>confirm new password</td>
          <td colspan="3">
            <el-form-item prop="password2">
              <el-input v-model="form.password2" ref="password2" style="width:300px;" type="password"></el-input>
            </el-form-item>
          </td>
        </tr>
      </table>
      <div class="btnArea mt20 tac">
        <el-button type="primary" @click="submitForm('form')">Commit</el-button>
        <el-button @click="backMain()" v-if="!isToLogin">Back</el-button>
        <el-button @click="returnMyMain()" v-if="isToLogin">Back</el-button>
      </div>
    </el-form>
    <!--table布局-->
    <div class="tableForm mt20" v-if="success">
      <div class="transactionStatus">
        <ynet-icon Type="transactionStatus" NO="1"></ynet-icon>
        <span class="ml15">password modify success！</span>
      </div>
      <div class="btnArea mt20 tac">
        <el-button type="primary" @click="returnMyMain()">return login</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'modifyPwd',
  components: {
  },
  data() {
    return {
      roleNo:'',
      isToLogin:false,
      data: '',
      modify: true,
      success: false,
      userName: '',
      mobileNo:'',
      message: {
        params: {
          mobileNo: '',
          tempType: 'CXPWD',
          phoneLen: '',
          transCode: 'IM000204'
        }
      },
      form: {
        userNo: '',//用户号
        password: '',//密码
        password1: '',
        password2: '',
        randomNum: '',
        randomNum1: '',
        randomNum2: '',
        transCode: 'IM000204'
      },
      rules: {
        password1: [
          {required:true,message:'required',trigger:'blur'},
          { validator: this.pwdValidator, vm: this, formName: "form"}
        ],
        password2: [
          {required:true,message:'required',trigger:'blur'},
          { validator: this.pwdValidator, vm: this, formName: "form"}
        ],
      },
      formName2Disabled: true,
    }
  },
  methods: {
    pwdValidator(rule, value, callback){
      if(this.form.password1!=this.form.password2 && this.form.password1!='' && this.form.password2!=''){
        callback("password not equal")
      }
    },
    dataBack(data) {
      //console.log(data)
      let _this = this
      _this.tempType = data.body.tempType
      _this.form.transCode = data.body.transCode
      //console.log(_this.tempType)
      let status = data.body.status
      let errorCode = data.body.errorCode
      let errorMsg = data.body.errorMsg
      if (status != 'S' || errorCode != '0') {
        _this.$alert(errorMsg, 'warning', {
          confirmButtonText: 'Confirm'
        })
      }
    },
    submitForm(form) {
      console.log(111111)
      let _this = this
      //提交表单前校验
      _this.modifyPassword()
      _this.$refs[form].validate((valid) => {
        console.log(3333)
        if (valid) {
          _this.modifyPassword()
        }else{
          console.log(222222)
        }
      })
    },

    modifyPassword() {
      let _this = this
      console.log("form:"+JSON.stringify(_this.form))
      _this.$tools.request(_this, "IM000204.do", _this.form).then(data => {
        let errorCode = data.body.errorCode
        let errorMsg = data.body.errorMsg
        if (errorCode == '' || errorCode == null || errorCode == '0') {
        	  // _this.$message('修改成功', '温馨提示', {
            //   confirmButtonText: '确定'
            // }).then(() => {
            //   _this.$router.push({path:'/login/loginMain'})
            // })
            _this.success=true
            //alert('修改成功')
            //_this.$router.push({path:'/login/loginMain'})
        } else {
          _this.$alert(errorMsg, 'warning', {
            confirmButtonText: 'Confirm'
          })
        }
      })
    },

    resetForm() {
      let _this = this
      _this.$refs['form'].resetFields()
    },
    getUserInfo() {
      let _this = this
      //console.log("密码修改页面"+_this.$tools.storage.get("user", "save"))
      let user = _this.$tools.storage.get("user", "save")
      _this.userName = user.userAlias
      _this.form.userNo = user.eCIFID
    },
    returnMyMain() {
        let _this = this
        _this.$router.push({path:'/login/loginMain'})
    },
    backMain(){
      //console.log(1234)
      let _this = this
      _this.$emit('back')
      //_this.$router.push({path:'/login/loginMain'})
    }
  },
  mounted() {
    let _this = this
    _this.getUserInfo()
    let user = _this.$tools.storage.get("user", "save")
    _this.roleNo = user.roleNo
    if (user) {
      if(user.resetFlag=='1'){
        //首次登录强制修改密码
        _this.isToLogin = true  
      }else{
        //修改密码
        _this.isToLogin = false 
      }
    }
  }
}
</script>
